{extend name='common/container'}
{block name="content"}
<div style="margin:0 15px 15px 15px;">
    <el-card shadow="never" style="min-height:650px;">
        <div>
            剩余可用账号   REC20:{{remaining_available_dapp}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TRC20:{{remaining_available_trc}}
        </div>
        <div class="btn-group" style="margin-top:10px;margin-bottom:10px;">
            <div>
                <el-button v-for="item in button_group" :key="item.access" v-if="checkPermission(item.access,'{:implode(',',session('admin.access'))}','{:session('admin.role_id')}',[1])" :disabled="$data[item.disabled]" :type="item.color" size="mini" :icon="item.icon" @click="fn(item.clickname)">
                    <span v-if="item.batch" v-text="$data['batchUpdateStatus']?'批量保存':'批量编辑'"></span>
                    <span v-else v-text="item.name"></span>
                </el-button>
            </div>
            <div><table-tool  @refesh_list="index"></table-tool></div>
        </div>
        <el-table row-key="id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" ref="multipleTable" border class="eltable" :data="list" style="width: 100%">
            <el-table-column align="center" type="selection" width="42"></el-table-column>
            <el-table-column align="center" type = '' property="id"  label="编号"  width="70">
            </el-table-column>
            <el-table-column align="center"  property="address" label="用户账号" width="250">
            </el-table-column>
            <el-table-column align="center"  property="real_address" label="实际钱包账号" width="250">
            </el-table-column>
            <el-table-column align="center"  property="type" :formatter="address_type" label="钱包类型" width="150">
            </el-table-column>
            <el-table-column align="center"  property="status" :formatter="status" label="账号分配情况">
            </el-table-column>
            <el-table-column align="center"  property="update_time" label="账号绑定时间">
            </el-table-column>
            <el-table-column align="center"  property="add_time" label="账号添加时间">
            </el-table-column>
            <el-table-column :fixed="ismobile()?false:'right'" label="操作" align="center" width="180">
                <template slot-scope="scope">
                    <div v-if="scope.row.id">
                        <el-button v-if="checkPermission('/admin/AccountAddress/edit.html','{:implode(",",session("admin.access"))}','{:session("admin.role_id")}',[1])" size="mini" icon="el-icon-edit" type="primary" @click="update(scope.row)" >修改</el-button>
                        <el-button v-if="checkPermission('/admin/AccountAddress/delete.html','{:implode(",",session("admin.access"))}','{:session("admin.role_id")}',[1]) && scope.row.status == 1" size="mini" icon="el-icon-delete" type="danger" @click="del(scope.row)" >删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
    </el-card>
</div>

<!--添加-->
<Add :show.sync="dialog.addDialogStatus" size="small" @refesh_list="index"></Add>
<!--修改-->
<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refesh_list="index"></Update>

{/block}
{block name="script"}
<script src="__PUBLIC__/assets/js/TronWeb.js"></script>
<script src="__PUBLIC__/assets/js/app.js"></script>
<script src="__PUBLIC__/assets/libs/xlsx/xlsx.core.min.js"></script>
<script src="__PUBLIC__/components/admin/account_address/add.js?v=<?php echo rand(1000,9999)?>"></script>
<script src="__PUBLIC__/components/admin/account_address/update.js?v=<?php echo rand(1000,9999)?>"></script>
<script>
    new Vue({
        el: '#app',
        components:{
        },
        data: function() {
            return {
                dialog: {
                    addDialogStatus : false,
                    updateDialogStatus : false,
                },
                searchData:{},
                detailInfo:{},
                button_group:[
                    {name:'添加',color:'success',access:'/admin/account_address/add.html',icon:'el-icon-plus',disabled:'',clickname:'add'},
                    {name:'新增TRC20(波场)钱包',color:'success',icon:'el-icon-plus',clickname:'increase_trc20'},
                    {name:'新增ECP(币安)钱包',color:'success',icon:'el-icon-plus',clickname:'increase_dapp'},
                ],
                loading: false,
                list:[],
                remaining_available_dapp:0,
                remaining_available_trc:0,
                updateInfo:{},
                page_data: {
                    limit: 20,
                    page: 1,
                    total:20,
                },

            }
        },
        created(){
            this.Remaining();
        },
        methods:{
            index(){
                let param = {limit:this.page_data.limit,page:this.page_data.page,order:this.order,sort:this.sort}
                Object.assign(param, this.searchData,this.urlobj)
                this.loading = true
                axios.post(base_url + '/AccountAddress/index',param).then(res => {
                    if(res.data.status == 200){
                        this.list = res.data.data.data
                        this.page_data.total = res.data.data.total
                        this.sum_amount = res.data.sum_amount
                        this.loading = false
                    }else{
                        this.$message.error(res.data.msg);
                    }
                })
            },
            add(){
                this.dialog.addDialogStatus = true
            },
            Remaining(){
                axios.post(base_url + '/AccountAddress/remaining_available',{}).then(res => {
                    this.remaining_available_dapp =  res.data.data.dapp_num;
                    this.remaining_available_trc = res.data.data.trc20_num;
                })
            },
            update(row){
                let id = row.id ? row.id : this.ids.join(',')
                axios.post(base_url + '/AccountAddress/getEdit',{id:id}).then(res => {
                    if(res.data.status == 200){
                        this.dialog.updateDialogStatus = true
                        this.updateInfo = res.data.data
                    }else{
                        this.$message.error(res.data.msg)
                    }
                })
            },
            del(row){
                this.$confirm('确定操作吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = row.id ? row.id : this.ids.join(',')
                    axios.post(base_url + '/AccountAddress/delete',{id:ids}).then(res => {
                        if(res.data.status == 200){
                            this.$message({message: res.data.msg, type: 'success'})
                            this.index()
                        }else{
                            this.$message.error(res.data.msg)
                        }
                    })
                }).catch(() => {})
            },
            status(row, column, cellValue){
                if (cellValue === 1){
                    return '未分配';
                }else if (cellValue === 2){
                    return '已分配';
                }
            },
            address_type(row, column, cellValue){
                if (cellValue === 0){
                    return '自定义无效账号';
                }else if (cellValue === 1){
                    return 'TRC20(波场)';
                }else if (cellValue === 2){
                    return 'ERC20(Dapp)';
                }
            },
            increase_dapp(){    //添加dapp钱包
                let abi = Ercbnb()
                window.web3 = new Web3(window.ethereum);
                const walletAll = Array();
                for (var i=0;i < 50;i++){
                    const wallet = window.web3.eth.accounts.create();
                    walletAll.push({'real_address':wallet.address,'secret_key':wallet.privateKey});
                }
                axios.post(base_url + '/AccountAddress/addDapp',{'data':walletAll}).then(res => {
                    if(res.data.status == 200){
                        this.$message({message: res.data.msg, type: 'success'})
                        this.index()
                    }else{
                        this.$message.error(res.data.msg)
                    }
                })

            },
            increase_trc20(){   //添加TRC20钱包
                axios.post(base_url + '/AccountAddress/addTrc20',{}).then(res => {
                    if(res.data.status == 200){
                        this.$message({message: res.data.msg, type: 'success'})
                        this.index()
                    }else{
                        this.$message.error(res.data.msg)
                    }
                })
            },
            fn(method){
                this[method](this.ids)
            },
        },
        mounted(){
            this.index()
        },
    })
</script>
{/block}
